package com.uduemc.biso.master.backend.service.impl;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.uduemc.biso.core.extities.center.AuthorizeModel;
import com.uduemc.biso.core.extities.center.AuthorizeModelAction;
import com.uduemc.biso.core.feign.AuthorizeModelActionFeign;
import com.uduemc.biso.core.feign.AuthorizeModelFeign;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.core.utils.ResultUtil;
import com.uduemc.biso.master.backend.service.AuthorizeService;

@Service
public class AuthorizeServiceImpl implements AuthorizeService {

	@Autowired
	private AuthorizeModelFeign authorizeModelFeign;

	@Autowired
	private AuthorizeModelActionFeign authorizeModelActionFeign;

	@Override
	public List<AuthorizeModel> getModelInfos()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = authorizeModelFeign.findAll();
		@SuppressWarnings("unchecked")
		List<AuthorizeModel> data = (List<AuthorizeModel>) ResultUtil.data(restResult,
				new TypeReference<List<AuthorizeModel>>() {
				});
		return data;
	}

	@Override
	public List<AuthorizeModelAction> getModelActionInfos()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = authorizeModelActionFeign.findAll();
		@SuppressWarnings("unchecked")
		List<AuthorizeModelAction> data = (List<AuthorizeModelAction>) ResultUtil.data(restResult,
				new TypeReference<List<AuthorizeModelAction>>() {
				});
		return data;
	}

	@Override
	public AuthorizeModelAction getAuthorizeModelActionInfoById(long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult findOne = authorizeModelActionFeign.findOne(id);
		AuthorizeModelAction data = ResultUtil.data(findOne, AuthorizeModelAction.class);
		return data;
	}

	@Override
	public List<AuthorizeModelAction> getModelActionInfosByModelId(long modelId)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = authorizeModelActionFeign.findByModelId(modelId);
		@SuppressWarnings("unchecked")
		List<AuthorizeModelAction> data = (List<AuthorizeModelAction>) ResultUtil.data(restResult,
				new TypeReference<List<AuthorizeModelAction>>() {
				});
		return data;
	}

	@Override
	public AuthorizeModelAction insertAuthorizeModelAction(AuthorizeModelAction insertAuthorizeModelAction)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		insertAuthorizeModelAction.setStatus((short) 0);
		RestResult insert = authorizeModelActionFeign.insert(insertAuthorizeModelAction);
		AuthorizeModelAction data = ResultUtil.data(insert, AuthorizeModelAction.class);
		return data;
	}

	@Override
	public AuthorizeModelAction updateAuthorizeModelAction(AuthorizeModelAction updateAuthorizeModelAction)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult update = authorizeModelActionFeign.updateById(updateAuthorizeModelAction);
		AuthorizeModelAction data = ResultUtil.data(update, AuthorizeModelAction.class);
		return data;
	}

	@Override
	public boolean deleteAuthorizeModelAction(long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = authorizeModelActionFeign.deleteById(id);
		int row = ResultUtil.data(restResult, Integer.class);
		return row > 0;
	}

}
